package com.doit.day01;


import java.sql.*;
import java.util.Iterator;

public class MysqlIterator implements Iterator<UserInfo> {

    private ResultSet resultSet;
    private UserInfo userInfo;


    public MysqlIterator() throws SQLException {
        Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "root", "123456");
        PreparedStatement pps = conn.prepareStatement("select * from user_info  ");
        //这一行代码代表的就是需要执行上面你写的pps里面的sql
         resultSet = pps.executeQuery();
        userInfo = new UserInfo();
    }


    @Override
    public boolean hasNext() {
        boolean flag = false;
        try {
            flag =  resultSet.next();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return flag;
    }


    @Override
    public UserInfo next() {
        try {
            int id = resultSet.getInt("id");
            String name = resultSet.getString("name");
            int age = resultSet.getInt("age");
            String gender = resultSet.getString("gender");
            userInfo.setName(name);
            userInfo.setGender(gender);
            userInfo.setId(id);
            userInfo.setAge(age);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return userInfo;
    }


}
